package com.kilomob.grady;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kilomob.grady.model.Book;
import com.kilomob.grady.service.BookService;
import com.kilomob.grady.service.BookService2;
import com.kilomob.grady.service.DemoService;

public class DemoClient {
    private static Logger LOGGER = LoggerFactory.getLogger(DemoClient.class);
    static DemoService demoService;
    static BookService bookService;
    static ClassPathXmlApplicationContext cac;

    public DemoClient() {
        cac = new ClassPathXmlApplicationContext("classpath:grady-demo-client.xml");
        demoService = (DemoService) cac.getBean("demoService");
        bookService = (BookService) cac.getBean("bookService");
    }

    public static void main(String[] args) throws InterruptedException {
        new DemoClient();
        long start = System.currentTimeMillis();
        String result = demoService.sayHello("world-0");
        LOGGER.debug("demoservice result:{},cost time:{}", result,
                (System.currentTimeMillis() - start));
        demoService.sayHello("world-1");
        
        start = System.currentTimeMillis();
        String br = bookService.save(new Book());
        LOGGER.debug("bookservice result:{},cost time:{}", br,
            (System.currentTimeMillis() - start));
        
        Thread.sleep(60*1000*3);
        start = System.currentTimeMillis();
        br = bookService.save(new Book());
        LOGGER.debug("bookservice result:{},cost time:{}", br,
            (System.currentTimeMillis() - start));
        
        cac.destroy();
    }

    public void request(int count) {
        for (int i = 0; i < count; i++) {
            Book book = new Book();
            book.setName("a");
            bookService.save(book);
        }
    }

}
